package com.summer.user.center.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import com.summer.common.file.FileSystemFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@Controller
@RequestMapping("/public")
public class PublicController {

    @Autowired
    private FileSystemFactory fileSystemFactory;

    @RequestMapping("/getHeadImage")
    public void getHeadImage(String path, HttpServletResponse response) throws IOException {

        if (StrUtil.isNotBlank(path)) {
            InputStream read = fileSystemFactory.getFileSystem().read(path);
            String suffix = path.substring(path.lastIndexOf(".") + 1);
            response.setContentType("image/" + suffix);
            IoUtil.copy(read, response.getOutputStream());
        }
    }
}
